package com.hyh.admin.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 处理上传文件不能立刻读取的问题
 * 每次需要重启服务器才能读取
 * @author hyh
 */
@Configuration
public class UploadConfig implements WebMvcConfigurer {

    @Value("${ad.profile}")
    private String uploadDir;

    @Value("${upload.avatar}")
    private String avatarDir;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目的绝对路径
        String projectPath = System.getProperty("user.dir");
        Path uploadPath = Paths.get(projectPath, uploadDir);
        Path avatarPath = Paths.get(projectPath, avatarDir);
        // 设置资源处理器
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadPath + "/");

        registry.addResourceHandler("/avatar/**")
                .addResourceLocations("file:" + avatarPath + "/");
    }
}
